package easier.app.x.view;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.RectF;

public class BitmapUtils {

    public static Bitmap toGrayscale( Bitmap bmpOriginal ) {

        int width, height;
        height = bmpOriginal.getHeight();
        width = bmpOriginal.getWidth();

        Bitmap bmpGrayscale = Bitmap.createBitmap( width, height, Bitmap.Config.RGB_565 );
        Canvas c = new Canvas( bmpGrayscale );
        Paint paint = new Paint();
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation( 0 );
        ColorMatrixColorFilter f = new ColorMatrixColorFilter( cm );
        paint.setColorFilter( f );
        c.drawBitmap( bmpOriginal, 0, 0, paint );
        return bmpGrayscale;
    }

    public static Bitmap convertToBlackWhite( Bitmap bmp ) {

        int width = bmp.getWidth(); // 获取位图的宽
        int height = bmp.getHeight(); // 获取位图的高
        int[] pixels = new int[width * height]; // 通过位图的大小创建像素点数组
        bmp.getPixels( pixels, 0, width, 0, 0, width, height );
        int alpha = 0xFF << 24;
        for ( int i = 0; i < height; i++ ) {
            for ( int j = 0; j < width; j++ ) {
                int grey = pixels[width * i + j];
                int red = ((grey & 0x00FF0000) >> 16);
                int green = ((grey & 0x0000FF00) >> 8);
                int blue = (grey & 0x000000FF);
                grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
                grey = alpha | (grey << 16) | (grey << 8) | grey;
                pixels[width * i + j] = grey;
            }
        }
        Bitmap newBmp = Bitmap.createBitmap( width, height, Config.RGB_565 );
        newBmp.setPixels( pixels, 0, width, 0, 0, width, height );
        return newBmp;

    }

    /**
     * 在图片上绘制文字
     *
     * @param originalImage 原始图片
     * @param watermarkText 添加的文字
     * @param config        配置一下文字的样式,位置
     *
     * @return 返回水印图片
     */
    public static Bitmap addTextToBitmap( Bitmap originalImage, String watermarkText, TextBitmapConfig config ) {

        if ( config == null ) {
            return null;
        }

        // 创建一个与原始图片相同大小的bitmap对象
        Bitmap bitmap = Bitmap.createBitmap( originalImage.getWidth(), originalImage.getHeight(), Bitmap.Config.ARGB_8888 );
        Canvas canvas = new Canvas( bitmap );
        // 在原始图片上绘制
        canvas.drawBitmap( originalImage, 0, 0, null );

        // 创建水印画笔
        Paint paint = config.providePaint();
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();

        // 在画布上绘制水印文字
        RectF rectF = new RectF( 0, 0, bitmap.getWidth(), bitmap.getHeight() );
        float[] xy = config.decideTextXY( rectF, paint );
        canvas.drawText( watermarkText, xy[0], xy[1] - fontMetrics.ascent, paint );

        // 返回包含水印的bitmap对象
        return bitmap;
    }

    public interface TextBitmapConfig {

        Paint providePaint();

        float[] decideTextXY( RectF rectF, Paint paint );
    }
}
